import { createRouter, createWebHashHistory } from 'vue-router'
import LoginForm from './components/LoginForm.vue';
import Register from './components/Register.vue';
import { TokenStore } from './store/user';
import component from 'element-plus/es/components/tree-select/src/tree-select-option.mjs';
import Home from './components/Home.vue';

//定义路由
const routers = [
    {
        path: '/',//访问该路由的URL路径
        name: 'Index',//路由名称
        component: Home//对应组件
        //component: () => import('./components//MyLibrary.vue')//对应组件
    },
    {
        path: '/login',//访问该路由的URL路径
        name: 'Login',//路由名称
        component: LoginForm//对应组件
    },
    {
        path: '/register',//访问该路由的URL路径
        name: 'Register',//路由名称
        component: Register//对应组件
    },
    {
        path: '/home',
        name: 'Home',
        component: () => import('./components/Home.vue'),
        meta: {
            needToken: true
        },
        children: [
            {
                path: 'blank',
                name: 'Blank',
                component: () => import('./components/Blank.vue')
            },
            {
                path: 'myLibrary',
                name: 'MyLibrary',
                component: () => import('./components/MyLibrary.vue'),
            },
            {
                path: 'library',
                name: 'Library',
                component: () => import('./components/Library.vue'),
            },
            {
                path:'AddArticle',
                name: 'AddArticle',
                component: () => import('./components/AddArticle.vue'),
            },
            {
                path: '/MyCollectionArticle',
                name: 'MyCollectionArticle',
                component: () => import('./components/MyCollectionArticle.vue'),
            },
            {
                path: 'MyCollectionLibrary',
                name: 'MyCollectionLibrary',
                component: () => import('./components/MyCollectionLibrary.vue'),
            },
            {
                path: 'Main',
                name: 'Main',
                component: () => import('./components/Main.vue'),
            },
            {
                path: 'History',
                name: 'History',
                component: () => import('./components/History.vue'),
            },
            {
                path: 'Search',
                name: 'Search',
                component: () => import('./components/Search.vue'),
            },
            {
                path: 'Follow',
                name: 'Follow',
                component: () => import('./components/Follow.vue'),
            }
        ]
    },
    {
        path: '/MyArticle',
        name: 'MyArticle',
        component: () => import('./components/MyArticle.vue'),
        meta: {
            needToken: true
        }
    },
    {
        path: '/UserInfo',
        name: 'UserInfo',
        component: () => import('./components/UserInfo.vue'),
        meta: {
            needToken: true
        }
    },
    {
        path: '/UserProfile',
        name: 'UserProfile',
        component: () => import('./components/UserProfile.vue')
    },   
    {
        path: '/HisLibrary',
        name: 'HisLibrary',
        component: () => import('./components/HisLibrary.vue')
    },
    {
        path: '/HisArticle',
        name: 'HisArticle',
        component: () => import('./components/HisArticle.vue')
    }
    
]

//创建路由实例
const router = createRouter({
    history: createWebHashHistory(),//
    routes: routers//将之前定义的路由配置数组 routers 传递给路由器。
})

//路由守卫
router.beforeEach((to, from, next) => {
    const store = TokenStore();
     if (to.matched.some(r=>r.meta?.needToken)) {
         if (!store.accessToken) {
             ElNotification({
                 title: '提示',
                 message: '请先登录',
                 type: 'warning',
                 duration: 2000
             })
            return next({name: 'Login',query: {redirect: to.fullPath}});
         }
     }
    //  if (to.matched.some(r => (r.meta?.isAuthCode && !isAuth(r.meta.isAuthCode as number)))) {
    //          ElNotification({
    //              title: '提示',
    //              message: '权限不足',
    //              type: 'warning',
    //              duration: 2000
    //          })
    //          return;
    //  }
     next();
 })

export default router//导出路由实例
